package com.itheima.admin.controller;

import com.itheima.admin.dto.ChannelPageRequestDto;
import com.itheima.admin.pojo.AdChannel;
import com.itheima.admin.service.AdChannelService;
import com.itheima.common.enums.HttpCodeEnum;
import com.itheima.common.vo.PageResultVo;
import com.itheima.common.vo.ResultVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.Date;

@RestController
@RequestMapping("/channel")
@Api(tags = "频道管理")
public class AdChannelController {
    @Autowired
    private AdChannelService adChannelService;

    /**
     * 频道分页查询
     * @param dto
     * @return
     */
    @PostMapping("/list")
    @ApiOperation("分页查询")
    public PageResultVo<AdChannel> pageList(@RequestBody ChannelPageRequestDto dto){
        PageResultVo<AdChannel> resultVo = adChannelService.pageList(dto);
        return resultVo;
    }

    /**
     * 添加频道
     * @param adChannel
     * @return
     */
    @PostMapping("/save")
    @ApiOperation("新建频道")
    public ResultVo save(@RequestBody AdChannel adChannel) {
        adChannel.setCreatedTime(LocalDateTime.now());
        adChannel.setIsDefault(0);
        if (adChannelService.save(adChannel)) {
            return ResultVo.ok();
        }
        return ResultVo.error();
    }

    /**
     * 修改频道
     * @param adChannel
     * @return
     */
    @PostMapping("/update")
    @ApiOperation("修改频道")
    public ResultVo update(@RequestBody AdChannel adChannel) {
        if (adChannel.getId()==null){
            return ResultVo.error();
        }
        if (adChannelService.updateById(adChannel)) {
            return ResultVo.ok();
        }
        return ResultVo.error();
    }

    /**
     * 删除频道
     * @param id
     * @return
     */
    @PostMapping("/del{id}")
    @ApiOperation("删除频道")
    public ResultVo delete(@PathVariable Integer id) {
        if (id==null){
            return ResultVo.error();
        }
        if (adChannelService.removeById(id)) {
            return ResultVo.ok();
        }
        return ResultVo.error();
    }

    /**
     * 通过id查找频道
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation("通过id查询频道")
    public ResultVo findById(@PathVariable Integer id) {
        AdChannel adChannel = adChannelService.getById(id);
        return ResultVo.ok(adChannel);
    }

}
